package com.flurry.sdk;

import com.flurry.sdk.kz;
import io.fabric.sdk.android.services.events.EventsFilesManager;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract class ky {
    private kh<jq> a = new kh<jq>() {
        public final /* synthetic */ void a(kg kgVar) {
            jq jqVar = (jq) kgVar;
            km.a(4, ky.this.b, "onNetworkStateChanged : isNetworkEnable = " + jqVar.a);
            if (jqVar.a) {
                ky.this.b();
            }
        }
    };
    public final String b;
    public Set<String> c = new HashSet();
    public la d;
    public String e = "defaultDataKey_";

    public interface a {
    }

    public abstract void a(byte[] bArr, String str, String str2);

    public ky(final String str, String str2) {
        this.b = str2;
        ki.a().a("com.flurry.android.sdk.NetworkStateEvent", this.a);
        jy.a().b(new ma() {
            public final void a() {
                ky.this.d = new la(str);
            }
        });
    }

    public final void b() {
        jy.a().b(new ma() {
            final /* synthetic */ a a = null;

            public final void a() {
                ky kyVar = ky.this;
                if (!jr.a().b) {
                    km.a(5, kyVar.b, "Reports were not sent! No Internet connection!");
                    return;
                }
                ArrayList<String> arrayList = new ArrayList<>(kyVar.d.c.keySet());
                if (arrayList.isEmpty()) {
                    km.a(4, kyVar.b, "No more reports to send.");
                    return;
                }
                for (String str : arrayList) {
                    if (kyVar.c()) {
                        List<String> a2 = kyVar.d.a(str);
                        km.a(4, kyVar.b, "Number of not sent blocks = " + a2.size());
                        for (String next : a2) {
                            if (!kyVar.c.contains(next)) {
                                if (!kyVar.c()) {
                                    break;
                                }
                                kz kzVar = (kz) new kf(jy.a().a.getFileStreamPath(kz.a(next)), ".yflurrydatasenderblock.", 1, new lj<kz>() {
                                    public final lg<kz> a(int i) {
                                        return new kz.a();
                                    }
                                }).a();
                                if (kzVar == null) {
                                    km.a(6, kyVar.b, "Internal ERROR! Cannot read!");
                                    kyVar.d.a(next, str);
                                } else {
                                    byte[] bArr = kzVar.b;
                                    if (bArr == null || bArr.length == 0) {
                                        km.a(6, kyVar.b, "Internal ERROR! Report is empty!");
                                        kyVar.d.a(next, str);
                                    } else {
                                        km.a(5, kyVar.b, "Reading block info " + next);
                                        kyVar.c.add(next);
                                        kyVar.a(bArr, next, str);
                                    }
                                }
                            }
                        }
                    } else {
                        return;
                    }
                }
            }
        });
    }

    public void a(final String str, final String str2, int i) {
        jy.a().b(new ma() {
            public final void a() {
                if (!ky.this.d.a(str, str2)) {
                    km.a(6, ky.this.b, "Internal error. Block wasn't deleted with id = " + str);
                }
                if (!ky.this.c.remove(str)) {
                    km.a(6, ky.this.b, "Internal error. Block with id = " + str + " was not in progress state");
                }
            }
        });
    }

    public final void a(final String str) {
        jy.a().b(new ma() {
            public final void a() {
                if (!ky.this.c.remove(str)) {
                    km.a(6, ky.this.b, "Internal error. Block with id = " + str + " was not in progress state");
                }
            }
        });
    }

    public final void b(final byte[] bArr, final String str, final String str2) {
        if (bArr == null || bArr.length == 0) {
            km.a(6, this.b, "Report that has to be sent is EMPTY or NULL");
            return;
        }
        jy.a().b(new ma() {
            public final void a() {
                ky kyVar = ky.this;
                byte[] bArr = bArr;
                kz kzVar = new kz(bArr);
                String str = kzVar.a;
                new kf(jy.a().a.getFileStreamPath(kz.a(str)), ".yflurrydatasenderblock.", 1, new lj<kz>() {
                    public final lg<kz> a(int i) {
                        return new kz.a();
                    }
                }).a(kzVar);
                km.a(5, kyVar.b, "Saving Block File " + str + " at " + jy.a().a.getFileStreamPath(kz.a(str)));
                kyVar.d.a(kzVar, kyVar.e + str + EventsFilesManager.ROLL_OVER_FILE_NAME_SEPARATOR + str2);
            }
        });
        b();
    }

    /* access modifiers changed from: package-private */
    public final boolean c() {
        return this.c.size() <= 5;
    }
}
